    #include "define.h"    
    #include "variable.h"   
       
    /*  write REG  */   
    #define IEBUSCTR         0x00   
       
    /*  
       MSB=0 表示命令  
       MSB=1 表示功能设置  
      
    */   
    #define IEBUSCMR         0x01   
       
       
    /*  
       本机地址  
              当作为主机通信时：作为主机地址  
              当作为从机通信时：作为从机地址  
    */   
    #define IEBUSUAR1        0x02   
    #define IEBUSUAR2        0x03   
       
    /*  
       远程地址：  
               假如是作为主机通信，则其表示远程的从机地址      
               假如是作为从机通信，则其表示远程的主机地址  
    */   
    #define IEBUSSAR1        0x04   
    #define IEBUSSAR2        0x05   
       
    #define IEBUSMCR         0x06   
    #define IEBUSTBF         0x0E   
       
    #define IEBUSSTREQ       0x01   
    #define IEBUSSRST        0x08   
    #define IEBUSREEN        0x10   
       
    /*  read REG   */   
    #define IEBUSSTR         0x00   
    #define IEBUSFLG         0x01   
       
    /* 作为主机接收数据的长度  */   
    #define IEBUSRDR1        0x02   
    /* 作为从机接收数据的长度  */   
    #define IEBUSRDR2        0x03   
       
    #define IEBUSLOR1        0x04   
    #define IEBUSLOR2        0x05   
    #define IEBUSDAR1        0x06   
    #define IEBUSDAR2        0x07   
    #define IEBUSRCR         0x08   
    #define IEBUSRBF         0x0E   
       
    #define IEBUSTFL         0x80   
    #define IEBUSTEP         0x40   
    #define IEBUSRFL         0x20   
    #define IEBUSREP         0x10   
       
    #define IEBUSMARQ        0x40   
    #define IEBUSSTRQ        0x20   
    #define IEBUSSLRE        0x10   
    #define IEBUSCEX         0x08   
    #define IEBUSRAW         0x04   
    #define IEBUSSTM         0x02   
    #define IEBUSIRQ         0x01   
       
    void IEBus_SendCmd(uchar address,uchar c)//write D72042 REG   
    {   
       uchar i;   
       address<<=4;//address   
       IEBusCMD_High;//cmd   
       NOP;   
       for(i=0;i<8;i++)   
         {   
           IEBusCLK_Low;   
           if(address&0x01)   
               IEBusDAT_High;   
           else   
               IEBusDAT_Low;   
           NOP;   
           IEBusCLK_High;   
           address>>=1;   
           NOP;   
          }   
       IEBusCMD_Low;//DATA   
       NOP;   
       for(i=0;i<8;i++)   
         {   
            IEBusCLK_Low;   
            if(c&0x01)   
               IEBusDAT_High;   
            else   
               IEBusDAT_Low;   
            NOP;   
            IEBusCLK_High;   
            c>>=1;   
            NOP;   
          }   
       NOP;   
       IEBusCMD_High;// CMD   
    }   
       
       
    void IEBus_Init(void)   
    {   
       IEBus_SendCmd(0x00,0x18);   
       Delay_ns(100);      
       IEBus_SendCmd(0x00,0x10);   
       IEBus_SendCmd(0x02,0x04);//UAR1 本地地址 04 360   
       IEBus_SendCmd(0x03,0x12);//UAR2   0x36   
       IEBus_SendCmd(0x01,0x80);//0x81 设置功能，MSB=0表示命令   
       IEBus_SendCmd(0x01,0x3B);   
    }     
       
    uchar IEBus_ReadStatus(uchar address)//Read D72042 REG   
    {   
       uchar i,c;   
       c=0;   
       address<<=4;   
       address|=0x08;   
       IEBusCMD_High;//cmd   
       NOP;   
       for(i=0;i<8;i++)   
         {   
           IEBusCLK_Low;   
           if(address&0x01)   
               IEBusDAT_High;   
           else   
               IEBusDAT_Low;        
           NOP;   
           IEBusCLK_High;   
           address>>=1;   
           NOP;   
          }   
       IEBusDATIn;//in   
       IEBusCMD_Low;   
       NOP;   
       for(i=0;i<8;i++)   
         {   
            IEBusCLK_Low;   
            c>>=1;   
            NOP;   
            IEBusCLK_High;   
            NOP;   
            if(IEBusDAT_Val)   
               c|=0x80;   
            else   
               c&=0x7F;   
          }   
       IEBusCMD_Low;   
       IEBusDATOut;   
       NOP;   
       return c;   
    }   
       
    //uchar RCR_Read(void)   
    //{   
    //   return IEBus_ReadStatus(IEBUSRCR);   
    //}    
       
    /***********************  
       send data function   
       n    send data length  
       p[]  send data array   
            p[0] BroadCast  
            p[1] high address 8bit  
            p[2] low  address 4bit & control byte 4bit  
            p[3] data length  
            p[4] to p[n] data   
    ************************/   
    void IEBus_SendData(uchar TBuff[])//test ok   
    {   
        uchar i;    
        uchar n;       
        IEBus_SendCmd(IEBUSSAR1,TBuff[2]+0x04);//set SAR1   
        IEBus_SendCmd(IEBUSSAR2,TBuff[1]);//set SAR2   
        IEBus_SendCmd(IEBUSMCR, TBuff[0]+0x2F);//set MCR(MSB=1)   
        if(!(IEBus_ReadStatus(IEBUSSTR)&IEBUSTEP))// not empty   
          {   
            while(IEBus_ReadStatus(IEBUSFLG)&IEBUSCEX);//waiting until CEX=0;   
            IEBus_SendCmd(IEBUSCMR,0x10);//clear TBuf   
            while(IEBus_ReadStatus(IEBUSFLG)&IEBUSCEX);//waiting until CEX=0;   
           }   
        n=TBuff[3];   
        IEBus_SendCmd(IEBUSTBF,TBuff[3]);//set data length   
        for(i=0;i<n;i++)   
          {   
            if(IEBus_ReadStatus(IEBUSSTR)&IEBUSTFL)// TBuf is full?   
               break;   
            IEBus_SendCmd(IEBUSTBF,TBuff[i+4]);   
           }   
        IEBus_SendCmd(IEBUSCMR,0x08);//send TBuf   
        while(IEBus_ReadStatus(IEBUSFLG)&IEBUSCEX);//waiting until CEX=0;   
           
        IEBus_SendCmd(IEBUSCMR,0x2F);//clear RBuf   
        while(IEBus_ReadStatus(IEBUSFLG)&IEBUSCEX);//waiting until CEX=0;   
        NOP;    
        NOP;   
        NOP;   
        IEBus_SendCmd(0x00,0x10);     
        //ClrBit(StatusFlag,IEBusTxFlg);//when send ok clear IEBusTxFlg    
        IEBusTxFlagClr;   
    }     
    /*   
      n         retrun the receive data length include address  
      RBuff     receive data buff  
      
      RBuff[0]  High Address 8bit  
      RBuff[1]  Low  Address 4bit & Control byte 4bit  
      RBuff[2]  Data Length  
      RBuff[3]  Data[0]  
      :::::::::::::::::  
      RBuff[n]  Data[n]  
    */   
    void IEBus_ReceiveData(uchar *n,uchar RBuff[])//reception data   
    {   
        uchar i;    
        uchar length;   
        length=IEBus_ReadStatus(IEBUSRDR2);//master receive data length;   
        *n=length;       
        NOP;   
        while(IEBus_ReadStatus(IEBUSFLG)&IEBUSCEX);//waiting until CEX=0; //0x01   
        for(i=0;i<length;i++)   
           {   
             while(IEBus_ReadStatus(IEBUSFLG)&IEBUSCEX);//waiting until CEX=0; //0x01   
             RBuff[i]=IEBus_ReadStatus(IEBUSRBF);   
             NOP;   
            }   
        IEBus_SendCmd(IEBUSCMR,0x2F);//clear RBuf   
        while(IEBus_ReadStatus(IEBUSFLG)&IEBUSCEX);//waiting until CEX=0;   
        NOP;   
        IEBus_SendCmd(0x00,0x10);   
    }       
       
    void IEBusDataCheck(void)   
    {   
       uchar c1,c2;     
       c1=IEBus_ReadStatus(0x00);   
       c2=IEBus_ReadStatus(0x01);   
       if(c2&0x04)   
        IEBus_Init();   
       if(!(c1&0x30))   
          {               
            c2=IEBus_ReadStatus(0x08);   
            c2&=0x0F;             
            if(c2==0x0A||c2==0x06)//broadcast && slave receive   
              {   
                IEBus_ReceiveData(&IEBus_RxDataLength,IEBus_RxBuf);        
                IEBusRxFlagSet;//set IEBusFlag to flag receive ok   
               }         
          }   
    }   